package com.example.hxd.zndzg.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hxd.zndzg.R;
import com.example.hxd.zndzg.entity.CardItem;

import java.util.ArrayList;
import java.util.List;

public class CardPagerAdapter extends PagerAdapter implements CardAdapter {

    private List<CardView> mViews;
    private List<CardItem> mData;
    private float mBaseElevation;
    private Context mContext;
    private int mCurrentDialogStyle = com.qmuiteam.qmui.R.style.QMUI_Dialog;
    private static List<Button> buttons;
    private CircleMenuListener mCircleMenuListener;
    private Activity mActivity;
    private ViewGroup mcontainer;

    private int[] btn_id = new int[]{R.id.drawer_1, R.id.drawer_2, R.id.drawer_3, R.id.drawer_4, R.id.drawer_5,
            R.id.drawer_6, R.id.drawer_7, R.id.drawer_8, R.id.drawer_9, R.id.drawer_10,
            R.id.drawer_11, R.id.drawer_12, R.id.drawer_13, R.id.drawer_14, R.id.drawer_15,
            R.id.drawer_16, R.id.drawer_17, R.id.drawer_18, R.id.drawer_19, R.id.drawer_20,
            R.id.drawer_21, R.id.drawer_22, R.id.drawer_23, R.id.drawer_24, R.id.drawer_25,
            R.id.drawer_26, R.id.drawer_27, R.id.drawer_28, R.id.drawer_29, R.id.drawer_30,
            R.id.drawer_31, R.id.drawer_32, R.id.drawer_33, R.id.drawer_34, R.id.drawer_35};

    private final int[] ITEM = new int[]{0, 1, 2, 3};

    public CardPagerAdapter() {
        buttons = new ArrayList<>();
        mData = new ArrayList<>();
        mViews = new ArrayList<>();
    }

    public void addCardItem(CardItem item, Context context, Activity activity) {
        mContext = context;
        mActivity = activity;
        mViews.add(null);
        mData.add(item);
    }

    public float getBaseElevation() {
        return mBaseElevation;
    }

    @Override
    public CardView getCardViewAt(int position) {
        return mViews.get(position);
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        View view = LayoutInflater.from(container.getContext()).inflate(R.layout.adapter_boxs, container, false);
        container.addView(view);
        mcontainer = container;
        bind(position, view);
        CardView cardView = (CardView) view.findViewById(R.id.cardView);

        if (mBaseElevation == 0) {
            mBaseElevation = cardView.getCardElevation();
        }

        cardView.setMaxCardElevation(mBaseElevation * MAX_ELEVATION_FACTOR);
        mViews.set(position, cardView);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
        mViews.set(position, null);
    }

    private void bind(final int item, final View view) {
        TextView titleTextView = (TextView)view.findViewById(R.id.box_title);
        for (int i = 0; i < 35; i++) {
            final int position = i + 1;
            buttons.add((Button)view.findViewById(btn_id[i]));
            buttons.get(item * 35 + i).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    buttons.get(item * 35 + position - 1).startAnimation(scaleBigAnim(600));
                    buttons.get(item * 35 + position - 1).setBackgroundResource(R.color.btn_ghost_blue_border_normal);
                    //按钮获取焦点
                    buttons.get(item * 35 + position - 1).setFocusable(false);
                    buttons.get(item * 35 + position - 1).setFocusableInTouchMode(false);
                    buttons.get(item * 35 + position - 1).requestFocus();
                    //使当前的Card变为灰色
                    mViews.get(item).setCardBackgroundColor(ContextCompat.getColor(mContext, R.color.qmui_config_color_gray_9));
                    mCircleMenuListener.onCircleMenuClick(item, position);
                }
            });
        }
        titleTextView.setText("当前为：" + (item + 1) + "号柜");
    }

    /**
     * 单击子按钮，当前子按钮变大并消失动画方法
     */
    private Animation scaleBigAnim(int time)
    {   //设置动画集合
        AnimationSet animationSet = new AnimationSet(true);
        //缩放动画
        ScaleAnimation scaleAnim = new ScaleAnimation(0.5f, 1.5f, 0.5f, 1.5f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
        //透明度动画
        AlphaAnimation alphaAnim = new AlphaAnimation(1f, 1f);
        animationSet.addAnimation(scaleAnim);   //添加缩放动画
        animationSet.addAnimation(alphaAnim);   //添加透明度动画
        animationSet.setDuration(time);         //设置动画时间
        animationSet.setFillAfter(false);        //停止最后一帧的位置
        return animationSet;
    }

    public interface CircleMenuListener {
        public void onCircleMenuClick(int item, int position);
    }

    public void setOnCircleMenuClickListener (CircleMenuListener  clickListener) {
        this.mCircleMenuListener = clickListener;
    }



    /**
     * 设置按钮是否可点击
     * @param isAble
     */
    public void setButtonClickable(boolean isAble){
        if (isAble){
            for (int i = 0; i < buttons.size(); i++) {
                buttons.get(i).setClickable(false);
            }
        }else {
            for (int i = 0; i < buttons.size(); i++) {
                buttons.get(i).setClickable(true);
            }
        }
    }

    /**
     * 还原颜色
     * @param item
     * @param position
     */
    public void setBackgroundColor(int item, final int position){
        buttons.get(item * 35 + position - 1).setBackgroundResource(R.drawable.btn_style_bule);
        mViews.get(item).setCardBackgroundColor(ContextCompat.getColor(mContext, R.color.white));
    }
}
